// 2010-03-17 - M. Caputo

#include <iostream>
#include <fstream>

#include "OffScene.h"

OffScene::OffScene(const QString & fileName)
{
	parseFile(fileName);
	colored = false;
}

OffScene::OffScene(const QString & fileName, const QColor & color)
{
	parseFile(fileName);
	colored = true;
	objectColor = color;
}

void OffScene::parseFile(const QString & fileName)
{
	int vCount, pCount, eCount;
	std::string str;
	
	std::ifstream ifs(fileName.toStdString().c_str());

	ifs >> str;	
	ifs >> vCount >> pCount >> eCount;
	
	CVertex **vertices = new CVertex*[vCount];
	for(int i=0; i<vCount; i++) {
		float x, y, z;
		ifs >> x >> y >> z;
		//std::cout << "Vertex data: " << x << " " << y << " " << z << "\n";
		vertices[i] = new CVertex(x, y, z);
	}
	
	for(int i=0; i<pCount; i++) {
		int n;
		ifs >> n;
		//std::cout << "Polygon size " << n << "\nPolygon data: ";
		CPolygon polygon;
		for(int j=0; j<n; j++) {
			int k;
			ifs >> k;
			//std::cout << k << " ";
			polygon.addVertex(*(vertices[k]));
		}
		//std::cout << "\n";
		addPolygon(polygon);
	}
	
	for(int i=0; i<vCount; i++) {
		delete vertices[i];
	}
	
	delete[] vertices;
	ifs.close();
}

void OffScene::addPolygon(const CPolygon & p)
{
	polygons.append(p);
}

int OffScene::polygonsCount()
{
	return polygons.size();
}

const CPolygon & OffScene::polygon(int i)
{
	return polygons[i];
}

bool OffScene::hasColor()
{
	return colored;
}

const QColor & OffScene::color()
{
	return objectColor;
}
